home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Utils / Software6 / Product13 / googlebar-0.9.5.06-fx.xpi / chrome / googlebar.jar / content / googlebarUtil.js < prev    next >
Text File  |  2005-02-21  |  7KB  |  243 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Googlebar for Mozilla.
  15.  *
  16.  * The Initial Developer of the Original Code is Andy Edmonds.
  17.  * Portions created by the Initial Developer are Copyright (C) 2001
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Andy Boughton
  22.  *  Andrew Houghton
  23.  *  John Woods
  24.  *  Bernd Kuemmerlen
  25.  *  Robert Mulcahy
  26.  *  Alfred Kayser
  27.  *  Reflex
  28.  *  Gary Turnbull
  29.  *  Raj Bhaskar
  30.  *  Joachim Thewes
  31.  *  Henrik Gemal
  32.  *  Robert Fernandes
  33.  *  Joe Chellman
  34.  *  Franki Cheung
  35.  *  Alban Fonrouge
  36.  *  Martin Hassman
  37.  *  Ufuk Kayserilioglu
  38.  *  Yoni Gilad
  39.  *  Tim Schmidt
  40.  *  timeless
  41.  *  Francis Turner
  42.  *
  43.  * Alternatively, the contents of this file may be used under the terms of
  44.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  45.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  46.  * in which case the provisions of the GPL or the LGPL are applicable instead
  47.  * of those above. If you wish to allow use of your version of this file only
  48.  * under the terms of either the GPL or the LGPL, and not to allow others to
  49.  * use your version of this file under the terms of the MPL, indicate your
  50.  * decision by deleting the provisions above and replace them with the notice
  51.  * and other provisions required by the GPL or the LGPL. If you do not delete
  52.  * the provisions above, a recipient may use your version of this file under
  53.  * the terms of any one of the MPL, the GPL or the LGPL.
  54.  *
  55.  * ***** END LICENSE BLOCK ***** */
  56.  
  57. const GB_CONSOLE_SVC = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
  58.  
  59. const GB_DEBUG_DUMP = false;    // Enable to see dump output - see logMessage function
  60. const GB_DEBUG_MSG  = true;        // Enable to see logs in js console message secion - see logMessage function
  61.  
  62. var myGooglebarUtil = {
  63.  
  64.     /*** Variable Declarations ***/
  65.     
  66.     mInFirefox: false,
  67.  
  68.     mInPrefs: false,
  69.  
  70.     /*** Function Declarations ***/
  71.  
  72.     buildUrl: function(aPathArray, aOffset) {
  73.         var mystr = '';
  74.  
  75.         for (var i = 0; i < aOffset; i++) {
  76.             if (aPathArray[i].length) {
  77.                 mystr += aPathArray[i];
  78.                 mystr += "/";
  79.                 
  80.                 if (i < (aOffset - 1)) {
  81.                     if (i == 0) {                     
  82.                         if (aPathArray[i] == "file:") {
  83.                             mystr += "/";
  84.                         }
  85.  
  86.                         // if it's the first item (protocol) then add a second /
  87.                         if (i == 0) {
  88.                             mystr += "/";
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     
  95.         return mystr;
  96.     },
  97.     
  98.     /*
  99.         A convenience function to get googlebar elements regardless of where
  100.         the function is being called from within the googlebar framework
  101.         (toolbar, popups, dialogs, prefs, etc.)  Uses the inPrefs flag to
  102.         attempt to find the element from opener first, otherwise it attempts
  103.         to first use the current document.
  104.     */
  105.     getAnonymousElement: function(aParent, aAttr, aValue) {
  106.         if (!aParent || !aAttr || !aValue) { return null; }
  107.         
  108.         var doc, element;
  109.         
  110.         try {
  111.             doc = this.isInPrefs() ? top.opener.document : document;
  112.             element = doc.getAnonymousElementByAttribute(aParent, aAttr, aValue);
  113.         }
  114.         catch (e) {
  115.             element = null;
  116.         }
  117.         
  118.         if (element != null) { return element; }
  119.         
  120.         try {
  121.             doc = this.isInPrefs() ? document : top.opener.document;
  122.             element = doc.getAnonymousElementByAttribute(aParent, aAttr, aValue);
  123.         }
  124.         catch (e) {
  125.             element = null;
  126.         }
  127.         
  128.         return element;
  129.     },
  130.  
  131.     /*
  132.         A convenience function to get googlebar elements regardless of where
  133.         the function is being called from within the googlebar framework
  134.         (toolbar, popups, dialogs, prefs, etc.)  Use the inPrefs flag to
  135.         attempt to find the element from opener first, otherwise it attempts
  136.         to first use the current document.
  137.     */
  138.     getElement: function(aId) {
  139.         if (!aId) { return null; }
  140.         
  141.         var doc, element;
  142.         
  143.         try {
  144.             doc = this.isInPrefs() ? top.opener.document : document;
  145.             element = doc.getElementById(aId);
  146.         }
  147.         catch (e) {
  148.             element = null;
  149.         }
  150.         
  151.         if (element != null) { return element; }
  152.     
  153.         try {
  154.             doc = this.isInPrefs() ? document : top.opener.document;
  155.             element = doc.getElementById(aId);
  156.         }
  157.         catch (e) {
  158.             element = null;
  159.         }
  160.         
  161.         return element;
  162.     },
  163.  
  164.     /*
  165.         A convenience method which uses the getElement function
  166.         to retrieve an element from anywhere within the googlebar
  167.         framework (toolbar, popups, dialogs, prefs, etc.) and
  168.         get an attribute value from that element.
  169.     */
  170.     getAttribute: function(aId, aAttr) {
  171.         if (!aId || !aAttr) { return null; }
  172.         
  173.         var element = this.getElement(aId);
  174.         
  175.         if (element == null) { return null; }
  176.     
  177.         return element.getAttribute(aAttr);
  178.     },
  179.     
  180.     inArray: function(aArray, aString) {
  181.         for (var i = 0; i < aArray.length; i++) {
  182.             if (aArray[i].toLowerCase() == aString.toLowerCase()) {
  183.                 return true;
  184.             }
  185.         }
  186.         return false;
  187.     },
  188.  
  189.     initialize: function() {
  190.         // TODO: Someday find a real way to do this besides looking for a null broadcaster
  191.         this.mInFirefox = this.getElement('googlebar_contextpref') == null;
  192.     },
  193.  
  194.     isInFirefox: function() {
  195.         return this.mInFirefox;
  196.     },
  197.     
  198.     isInPrefs: function() {
  199.         return this.mInPrefs;
  200.     },
  201.     
  202.     logMessage: function(aMessage) {
  203.         if (GB_DEBUG_DUMP) { dump('Googlebar: ' + aMessage); }
  204.         if (GB_DEBUG_MSG) { GB_CONSOLE_SVC.logStringMessage('Googlebar: ' + aMessage); }
  205.     },
  206.     
  207.     normalizeString: function(aString) {
  208.         // trim leading and trailing spaces
  209.         var newString = this.trimString(aString);
  210.     
  211.         // reduce runs of more than one space to one space only
  212.         newString = newString.replace(/(\s+)/g, ' '); // a space
  213.     
  214.         return newString;
  215.     },
  216.  
  217.     setInPrefs: function(aBoolean) {
  218.         if (aBoolean == true || aBoolean == "true") {
  219.             this.mInPrefs = true;
  220.         }
  221.         else {
  222.             this.mInPrefs = false;
  223.         }
  224.     },
  225.  
  226.     trimString: function(aString) {
  227.         if (!aString) {
  228.             return "";
  229.         }
  230.         
  231.         return aString.replace(/(^\s+)|(\s+$)/g, '');
  232.     },
  233.     
  234.     updateClass: function (aElement, aPosition, aNewClass) {
  235.         var oldClass = aElement.getAttribute("class");
  236.         var classes = oldClass.split(" ");
  237.         classes[aPosition] = aNewClass;
  238.         aElement.setAttribute("class", classes.join(" "));
  239.     }
  240. };
  241.  
  242. myGooglebarUtil.initialize();
  243.